﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.Common;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Departmentss
{
    public class SearchDepartmentAreaHandler : IRequestHandler<SearchDepartmentAreaCommand, Result<List<CascaderDto>>>
    {
        /// <summary>
        /// 部门区域
        /// </summary>
        private readonly IBaseRepository<DepartmentAreas> _departmentAreaRepository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchDepartmentAreaHandler> _logger;

        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="departmentAreaRepository"></param>
        /// <param name="logger"></param>
        /// <param name="mapper"></param>
        public SearchDepartmentAreaHandler(IBaseRepository<DepartmentAreas> departmentAreaRepository,
            ILogger<SearchDepartmentAreaHandler> logger,
            IMapper mapper)
        {
            _departmentAreaRepository = departmentAreaRepository;
            _logger = logger;
            _mapper = mapper;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<List<CascaderDto>>> Handle(SearchDepartmentAreaCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<CascaderDto>>() { Code = (int)CommonErrorCode.Search };
            try
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Data = GetCascaderArea(request.Pid);
                result.Msg = "获取部门成功";
            }
            catch (Exception ex)
            {
                result.Msg = "获取部门区域异常" + ex.Message;
                _logger.LogError("获取部门区域异常" + ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 级联获取部门区域
        /// </summary>
        /// <param name="Pid"></param>
        /// <returns></returns>
        public List<CascaderDto> GetCascaderArea(int Pid)
        {
            var cascader = new List<CascaderDto>();
            var departmentArea = _departmentAreaRepository.Query().ToList();
            cascader = departmentArea.Where(z => z.PId == Pid).Select(z => new CascaderDto()
            {
                Value = z.DepartmentAreaId,
                Label = z.DepartmentAreaName,
                Children = GetCascaderArea(z.DepartmentAreaId).Count > 0 ? GetCascaderArea(z.DepartmentAreaId) : new List<CascaderDto>()
            }).ToList();
            return cascader;
        }
    }
}
